import { Component, OnInit } from '@angular/core';
import { Service } from '../../../server/server';
import { tablestructureDatas } from '../database.model';
import { DatabaseService } from '../database.service';

@Component({
  selector: 'app-database-update',
  templateUrl: './database-update.component.html'
})
export class DatabaseUpdateComponent implements OnInit {

  constructor(private server: Service, private databaseService: DatabaseService) { }

  ngOnInit() {
    this.initData();
    // this.tabledata = {
    //   table_name:'dh_resource_categorys',
    //   tabel_desc:'资源分类表'
    // }

    //接收详情数据
    this.databaseupdateeventEmit = this.server.databasedetailseventEmit.subscribe((value: any) => {
      this.details = value;
      //console.log(this.details)
      this.terminals = this.details.tablesColRelations;
    });
  }

  //取消订阅
  public databaseupdateeventEmit: any;
  ngOnDestroy() {
    this.databaseupdateeventEmit ? this.databaseupdateeventEmit.unsubscribe() : '';
  }

  //数据初始化
  public initData() {
    this.terminals = this.databaseService.terminals;
    this.typelist = this.databaseService.typeDate();
    //console.log(this.terminals, this.typelist)
  }

  //上移下移
  public upDown(val, i, terminals) {
    this.databaseService.upDown(val, i, terminals);
  }

  public details: any;
  public submitUpdate(val) {
    //console.log(val)
    let tables = { tableDesc: val.table_desc, tableName: val.table_name, tableId: val.table_Id }
    //console.log(tables)
    //console.log(this.terminals)
    this.databaseService.access(2, val.table_Id, tables, this.terminals).then((res: any) => {
      //console.log(res)
    })
  }

  public typelist: any;

  public terminals: Array<tablestructureDatas>;


  //添加一行
  addTr() {
    this.databaseService.addTr();
    this.terminals = this.databaseService.terminals;
  }

  public mainArray() {
    this.databaseService.mainArray();
  }

  //主键单选
  public primaryKeymin(i, val) {
    this.databaseService.primaryKeymin(i);
  }
  deletetr(i) {
    this.databaseService.deletetr(i);
  }

  //控制输入
  typeChange(event, item, i) {
    item.columeType = event.target.value;
    let type = this.databaseService.terminals[i].columeType;
    if (type == "double" || type == "float" || type == "decimal" || type == "numeric") {
      this.databaseService.terminals[i].columeXsdtf = 1;
    } else {
      this.databaseService.terminals[i].columeXsdtf = 0;
    }
  }


  // nameChange(event, item) {
  //   item.column_name = event.target.value;
  // }

  // lengthChange(event, item) {
  //   item.column_length = event.target.value;
  // }
  // xsdChange(event, item) {
  //   item.column_xsd = event.target.value;
  // }
  // notnullChange(event, item) {
  //   item.column_not_null = event.target.value;
  // }
  // orderChange(event, item) {
  //   item.column_order = event.target.value;
  // }
  // defaultChange(event, item) {
  //   event.target.value = item.column_default;
  // }

}
